//
//  ChatManager.swift
//  TanTan
//
//  Created by 于航 on 2024/9/16.
//

import Foundation
import Combine
import UIKit

class ChatManager : ObservableObject {
    @Published var messages: [Message] = []
    @Published var keyboardIsShowing: Bool = false
    var keyboardPublisher: AnyCancellable? = nil
    var user : User
    
    init(user: User) {
        self.user = user
        loadMessage()
        setUpPublishers() 
    }
    
    func setUpPublishers() {
        keyboardPublisher = Publishers.Merge(
            NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)
                .map{_ in true},
            NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)
                .map{_ in false}
        )
        .subscribe(on: DispatchQueue.main)
        .assign(to: \.keyboardIsShowing, on: self)
    }
    
    func sendMessage(_ message : Message) {
        messages.append(message)
    }
    
    //mock network request
    
    func loadMessage() {
        messages = [Message.messageSend, Message.messageRecv]
    }
}
